package com.wbxqy.car_management_system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wbxqy.car_management_system.entity.Car;
import com.wbxqy.car_management_system.entity.CarRecord;
import com.wbxqy.car_management_system.entity.CarRequest;
import com.wbxqy.car_management_system.service.ICarRecordService;
import com.wbxqy.car_management_system.service.ICarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 车辆维修记录表 前端控制器
 * </p>
 *
 * @author wbxqy
 * @since 2024-07-20
 */
@RestController
@RequestMapping("/wbxqy/carRecord")
public class CarRecordController {

    @Autowired
    private ICarRecordService carRecordService;
    @Autowired
    private ICarService carService;

    @PostMapping
    public boolean addCarRecord(@RequestBody CarRecord carRecord) {
        return carRecordService.save(carRecord);
    }

    @DeleteMapping("/{id}")
    public boolean deleteCarRecord(@PathVariable Integer id) {
        return carRecordService.removeById(id);
    }

    @PutMapping
    public boolean updateCarRecord(@RequestBody CarRecord carRecord) {
        return carRecordService.updateById(carRecord);
    }

    @GetMapping("/{id}")
    public CarRecord getCarRecordById(@PathVariable Integer id) {
        return carRecordService.getById(id);
    }

    @GetMapping("/page")
    public Page<CarRecord> getAllCarRecordsPage(@RequestParam(required = false, defaultValue = "1") Integer page,
                                                @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                                                @RequestParam(required = false) String carId,
                                                @RequestParam(required = false) String recordDate,
                                                @RequestParam(required = false) String recordCate) {
        // 构造分页构造器
        final Page<CarRecord> pageInfo = new Page<>(page, pageSize);
        final QueryWrapper<CarRecord> queryWrapper = new QueryWrapper<>();

        // 添加过滤条件
        if (carId != null && !carId.isEmpty()) {
            queryWrapper.like("car_id_wbxqy", carId);
        }
        if (recordDate != null && !recordDate.isEmpty()) {
            queryWrapper.like("record_date_wbxqy", recordDate);
        }
        if (recordCate != null && !recordCate.isEmpty()) {
            queryWrapper.eq("record_cate_wbxqy", recordCate);
        }

        // 执行分页查询
        final Page<CarRecord> carRecordPage = carRecordService.page(pageInfo, queryWrapper);
        for(CarRecord carRecord:carRecordPage.getRecords()) {
            Car car = null;
            if (carRecord.getCarIdWbxqy() != null) {
                car = carService.getById(carRecord.getCarIdWbxqy());
            }
            carRecord.setCar(car);
        }
        return pageInfo;
    }
}
